library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
"%&%" = function(a,b) paste(a,b,sep="")

for(pop in c('AFA-CAU','AFA-HIS','CAU-HIS')){
  res <- read.table(pop %&% '_MESA_Nk-20.local-h2_gen-corr_EXP-SIM-0.2018-03-25.txt',header=T)
  res <- dplyr::mutate(res,min.h2=ifelse(pop1.h2<pop2.h2,pop1.h2,pop2.h2), max.h2=ifelse(pop1.h2>pop2.h2,pop1.h2,pop2.h2))
  print(summary(res))
  print(ggplot(res, aes(x=rG,y=pop1.h2)) + geom_point() + ggtitle(pop))
  print(ggplot(res, aes(x=rG,y=pop1.se)) + geom_point() + ggtitle(pop))
  print(ggplot(res, aes(x=pop1.h2,y=pop1.se)) + geom_point() + ggtitle(pop))
  print(ggplot(res, aes(x=rG,y=pop2.h2)) + geom_point() + ggtitle(pop))
  print(ggplot(res, aes(x=rG,y=pop2.se)) + geom_point() + ggtitle(pop))
  print(ggplot(res, aes(x=pop2.h2,y=pop2.se)) + geom_point() + ggtitle(pop))
  print(ggplot(res, aes(x=pop1.h2,y=pop2.h2,col=rG)) + geom_point()+scale_color_gradientn(colours = rainbow(4)) + ggtitle(pop))
  print(ggplot(res, aes(x=pop1.h2,y=pop2.h2)) + stat_density_2d(aes(fill = ..level..), geom = "polygon") + ggtitle(pop))
  print(ggplot(res, aes(x=pop1.h2,y=pop2.h2,col=log10(rG.se))) + geom_point() +scale_color_gradientn(colours = rainbow(4)) + ggtitle(pop))
  
  print(ggplot(res, aes(x=rG,y=log10(rG.se),col=min.h2)) + geom_point(alpha=0.5)+scale_color_gradientn(colours = rainbow(4)) + ggtitle(pop))
  print(ggplot(res, aes(x=rG,y=log10(rG.se),col=max.h2)) + geom_point(alpha=0.5)+scale_color_gradientn(colours = rainbow(4)) + ggtitle(pop))
  print(ggplot(res, aes(x=rG,y=log10(rG.se))) + stat_density_2d(aes(fill = ..level..), geom = "polygon") +
    coord_cartesian(xlim=c(-1,1)) + ggtitle(pop))
  print(ggplot(res, aes(x=rG,y=log10(rG.se))) + stat_density_2d(geom = "point", aes(size = ..density..), n = 20, contour = FALSE) + ggtitle(pop))
  print(ggplot(res, aes(x=rG)) + geom_density() + ggtitle(pop))
  print(ggplot(res, aes(x=log10(rG.se))) + geom_density() + ggtitle(pop))
  print(ggplot(res, aes(x=pop1.h2)) + geom_density() + ggtitle(pop))
  print(ggplot(res, aes(x=pop2.h2)) + geom_density() + ggtitle(pop))
  print(ggplot(res, aes(x=rG,y=log10(rG.se))) + geom_bin2d() + ggtitle(pop))
  print(ggplot(res, aes(x=rG,y=log10(rG.se),col=pop1.h2)) + geom_point() + ggtitle(pop))
}
##                pop.data          N                      ensid     
##  AFA-CAU_MESA_Nk-20:9797   Min.   :811   ENSG00000000419.8 :   1  
##                            1st Qu.:811   ENSG00000000457.8 :   1  
##                            Median :811   ENSG00000000460.12:   1  
##                            Mean   :811   ENSG00000000938.8 :   1  
##                            3rd Qu.:811   ENSG00000000971.11:   1  
##                            Max.   :811   ENSG00000001036.8 :   1  
##                                          (Other)           :9791  
##       gene           chr           start                end           
##  A1BG   :   1   chr1   :1062   Min.   :    14363   Min.   :    29370  
##  AAAS   :   1   chr19  : 752   1st Qu.: 31191920   1st Qu.: 31230667  
##  AACS   :   1   chr2   : 657   Median : 57647548   Median : 57698944  
##  AAED1  :   1   chr17  : 618   Mean   : 72897113   Mean   : 72948622  
##  AAGAB  :   1   chr11  : 607   3rd Qu.:108320411   3rd Qu.:108507766  
##  AAK1   :   1   chr3   : 584   Max.   :249200395   Max.   :249214145  
##  (Other):9791   (Other):5517                                          
##     pop1.h2          pop1.se          pop2.h2          pop2.se      
##  Min.   :0.0000   Min.   :0.0103   Min.   :0.0000   Min.   :0.0036  
##  1st Qu.:0.0035   1st Qu.:0.0517   1st Qu.:0.0031   1st Qu.:0.0195  
##  Median :0.0457   Median :0.0658   Median :0.0273   Median :0.0269  
##  Mean   :0.1074   Mean   :0.0672   Mean   :0.1135   Mean   :0.0323  
##  3rd Qu.:0.1468   3rd Qu.:0.0833   3rd Qu.:0.1369   3rd Qu.:0.0445  
##  Max.   :0.9012   Max.   :0.1371   Max.   :0.9811   Max.   :0.1020  
##  NA's   :642      NA's   :642      NA's   :642      NA's   :642     
##        rG              rG.se               min.h2           max.h2      
##  Min.   :-1.0000   Min.   :        0   Min.   :0.0000   Min.   :0.0000  
##  1st Qu.:-1.0000   1st Qu.:        0   1st Qu.:0.0014   1st Qu.:0.0129  
##  Median : 0.0187   Median :        1   Median :0.0149   Median :0.0712  
##  Mean   : 0.0138   Mean   :   101988   Mean   :0.0745   Mean   :0.1465  
##  3rd Qu.: 1.0000   3rd Qu.:       15   3rd Qu.:0.0789   3rd Qu.:0.2021  
##  Max.   : 1.0000   Max.   :611803315   Max.   :0.9012   Max.   :0.9811  
##  NA's   :642       NA's   :642         NA's   :642      NA's   :642
## Warning: Removed 642 rows containing missing values (geom_point).

## Warning: Removed 642 rows containing missing values (geom_point).

## Warning: Removed 642 rows containing missing values (geom_point).

## Warning: Removed 642 rows containing missing values (geom_point).

## Warning: Removed 642 rows containing missing values (geom_point).

## Warning: Removed 642 rows containing missing values (geom_point).

## Warning: Removed 642 rows containing missing values (geom_point).

## Warning: Removed 642 rows containing non-finite values (stat_density2d).

## Warning: Removed 642 rows containing missing values (geom_point).

## Warning: Removed 642 rows containing missing values (geom_point).

## Warning: Removed 642 rows containing missing values (geom_point).

## Warning: Removed 642 rows containing non-finite values (stat_density2d).

## Warning: Removed 642 rows containing non-finite values (stat_density2d).

## Warning: Removed 642 rows containing non-finite values (stat_density).

## Warning: Removed 642 rows containing non-finite values (stat_density).

## Warning: Removed 642 rows containing non-finite values (stat_density).

## Warning: Removed 642 rows containing non-finite values (stat_density).

## Warning: Removed 642 rows containing non-finite values (stat_bin2d).

## Warning: Removed 642 rows containing missing values (geom_point).

##                pop.data          N                      ensid     
##  AFA-HIS_MESA_Nk-20:9797   Min.   :585   ENSG00000000419.8 :   1  
##                            1st Qu.:585   ENSG00000000457.8 :   1  
##                            Median :585   ENSG00000000460.12:   1  
##                            Mean   :585   ENSG00000000938.8 :   1  
##                            3rd Qu.:585   ENSG00000000971.11:   1  
##                            Max.   :585   ENSG00000001036.8 :   1  
##                                          (Other)           :9791  
##       gene           chr           start                end           
##  A1BG   :   1   chr1   :1062   Min.   :    14363   Min.   :    29370  
##  AAAS   :   1   chr19  : 752   1st Qu.: 31191920   1st Qu.: 31230667  
##  AACS   :   1   chr2   : 657   Median : 57647548   Median : 57698944  
##  AAED1  :   1   chr17  : 618   Mean   : 72897113   Mean   : 72948622  
##  AAGAB  :   1   chr11  : 607   3rd Qu.:108320411   3rd Qu.:108507766  
##  AAK1   :   1   chr3   : 584   Max.   :249200395   Max.   :249214145  
##  (Other):9791   (Other):5517                                          
##     pop1.h2          pop1.se          pop2.h2          pop2.se      
##  Min.   :0.0000   Min.   :0.0073   Min.   :0.0000   Min.   :0.0061  
##  1st Qu.:0.0042   1st Qu.:0.0507   1st Qu.:0.0033   1st Qu.:0.0352  
##  Median :0.0468   Median :0.0641   Median :0.0427   Median :0.0471  
##  Mean   :0.1041   Mean   :0.0657   Mean   :0.1270   Mean   :0.0500  
##  3rd Qu.:0.1400   3rd Qu.:0.0808   3rd Qu.:0.1767   3rd Qu.:0.0661  
##  Max.   :0.8783   Max.   :0.1518   Max.   :0.9395   Max.   :0.0990  
##  NA's   :598      NA's   :598      NA's   :598      NA's   :598     
##        rG              rG.se               min.h2           max.h2      
##  Min.   :-1.0000   Min.   :        0   Min.   :0.0000   Min.   :0.0000  
##  1st Qu.:-0.9050   1st Qu.:        0   1st Qu.:0.0015   1st Qu.:0.0154  
##  Median : 0.1513   Median :        1   Median :0.0198   Median :0.0793  
##  Mean   : 0.0839   Mean   :   191081   Mean   :0.0811   Mean   :0.1500  
##  3rd Qu.: 1.0000   3rd Qu.:       17   3rd Qu.:0.0959   3rd Qu.:0.2128  
##  Max.   : 1.0000   Max.   :714108060   Max.   :0.8783   Max.   :0.9395  
##  NA's   :598       NA's   :598         NA's   :598      NA's   :598
## Warning: Removed 598 rows containing missing values (geom_point).

## Warning: Removed 598 rows containing missing values (geom_point).

## Warning: Removed 598 rows containing missing values (geom_point).

## Warning: Removed 598 rows containing missing values (geom_point).

## Warning: Removed 598 rows containing missing values (geom_point).

## Warning: Removed 598 rows containing missing values (geom_point).

## Warning: Removed 598 rows containing missing values (geom_point).

## Warning: Removed 598 rows containing non-finite values (stat_density2d).

## Warning: Removed 598 rows containing missing values (geom_point).

## Warning: Removed 598 rows containing missing values (geom_point).

## Warning: Removed 598 rows containing missing values (geom_point).

## Warning: Removed 598 rows containing non-finite values (stat_density2d).

## Warning: Removed 598 rows containing non-finite values (stat_density2d).

## Warning: Removed 598 rows containing non-finite values (stat_density).

## Warning: Removed 598 rows containing non-finite values (stat_density).

## Warning: Removed 598 rows containing non-finite values (stat_density).

## Warning: Removed 598 rows containing non-finite values (stat_density).

## Warning: Removed 598 rows containing non-finite values (stat_bin2d).

## Warning: Removed 598 rows containing missing values (geom_point).

##                pop.data          N                      ensid     
##  CAU-HIS_MESA_Nk-20:9797   Min.   :930   ENSG00000000419.8 :   1  
##                            1st Qu.:930   ENSG00000000457.8 :   1  
##                            Median :930   ENSG00000000460.12:   1  
##                            Mean   :930   ENSG00000000938.8 :   1  
##                            3rd Qu.:930   ENSG00000000971.11:   1  
##                            Max.   :930   ENSG00000001036.8 :   1  
##                                          (Other)           :9791  
##       gene           chr           start                end           
##  A1BG   :   1   chr1   :1062   Min.   :    14363   Min.   :    29370  
##  AAAS   :   1   chr19  : 752   1st Qu.: 31191920   1st Qu.: 31230667  
##  AACS   :   1   chr2   : 657   Median : 57647548   Median : 57698944  
##  AAED1  :   1   chr17  : 618   Mean   : 72897113   Mean   : 72948622  
##  AAGAB  :   1   chr11  : 607   3rd Qu.:108320411   3rd Qu.:108507766  
##  AAK1   :   1   chr3   : 584   Max.   :249200395   Max.   :249214145  
##  (Other):9791   (Other):5517                                          
##     pop1.h2          pop1.se          pop2.h2          pop2.se      
##  Min.   :0.0000   Min.   :0.0028   Min.   :0.0000   Min.   :0.0063  
##  1st Qu.:0.0041   1st Qu.:0.0202   1st Qu.:0.0024   1st Qu.:0.0296  
##  Median :0.0340   Median :0.0279   Median :0.0353   Median :0.0400  
##  Mean   :0.1106   Mean   :0.0325   Mean   :0.1032   Mean   :0.0440  
##  3rd Qu.:0.1368   3rd Qu.:0.0445   3rd Qu.:0.1319   3rd Qu.:0.0583  
##  Max.   :0.9858   Max.   :0.1170   Max.   :0.9739   Max.   :0.1060  
##  NA's   :334      NA's   :334      NA's   :334      NA's   :334     
##        rG              rG.se               min.h2           max.h2      
##  Min.   :-1.0000   Min.   :        0   Min.   :0.0000   Min.   :0.0000  
##  1st Qu.:-0.7188   1st Qu.:        0   1st Qu.:0.0013   1st Qu.:0.0113  
##  Median : 0.0333   Median :        1   Median :0.0158   Median :0.0576  
##  Mean   : 0.0210   Mean   :    55764   Mean   :0.0836   Mean   :0.1302  
##  3rd Qu.: 0.8230   3rd Qu.:       13   3rd Qu.:0.0925   3rd Qu.:0.1726  
##  Max.   : 1.0000   Max.   :273744693   Max.   :0.9739   Max.   :0.9858  
##  NA's   :334       NA's   :334         NA's   :334      NA's   :334
## Warning: Removed 334 rows containing missing values (geom_point).

## Warning: Removed 334 rows containing missing values (geom_point).

## Warning: Removed 334 rows containing missing values (geom_point).

## Warning: Removed 334 rows containing missing values (geom_point).

## Warning: Removed 334 rows containing missing values (geom_point).

## Warning: Removed 334 rows containing missing values (geom_point).

## Warning: Removed 334 rows containing missing values (geom_point).

## Warning: Removed 334 rows containing non-finite values (stat_density2d).

## Warning: Removed 334 rows containing missing values (geom_point).

## Warning: Removed 334 rows containing missing values (geom_point).

## Warning: Removed 334 rows containing missing values (geom_point).

## Warning: Removed 334 rows containing non-finite values (stat_density2d).

## Warning: Removed 334 rows containing non-finite values (stat_density2d).

## Warning: Removed 334 rows containing non-finite values (stat_density).

## Warning: Removed 334 rows containing non-finite values (stat_density).

## Warning: Removed 334 rows containing non-finite values (stat_density).

## Warning: Removed 334 rows containing non-finite values (stat_density).

## Warning: Removed 334 rows containing non-finite values (stat_bin2d).

## Warning: Removed 334 rows containing missing values (geom_point).